# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
pkgname=open-vm-tools
pkgver=12.3.0
_pkgsubver=${pkgver#*_p}
_ver=${pkgver/_p/-}
pkgrel=0
pkgdesc="The Open Virtual Machine Tools are the open source implementation of VMware Tools."
url="https://github.com/vmware/open-vm-tools/"
arch="aarch64 x86 x86_64"
arch="$arch !x86" # Fails to compile with new musl
license="LGPL-2.1-or-later"
subpackages="
	$pkgname-dbg
	$pkgname-lang
	$pkgname-deploypkg
	$pkgname-hgfs
	$pkgname-guestinfo
	$pkgname-gtk
	$pkgname-vix
	$pkgname-static
	$pkgname-timesync
	$pkgname-vmbackup
	$pkgname-plugins-all:_all:noarch
	$pkgname-dev
	$pkgname-openrc
	$pkgname-doc
	"
install="$pkgname.pre-install $pkgname.post-upgrade"
checkdepends="cunit checkbashisms"
makedepends="
	autoconf
	automake
	bash
	doxygen
	fuse3-dev
	gdk-pixbuf-xlib-dev
	glib-dev
	gtk+3.0-dev
	gtkmm3-dev
	libice-dev
	libmspack-dev
	libsm-dev
	libtirpc-dev
	libtool
	libxext-dev
	libxinerama-dev
	libxrandr-dev
	libxtst-dev
	linux-headers
	openssl-dev>3
	procps-compat-dev
	rpcgen
	"
pkggroups="vmware"
source="$pkgname-$pkgver.tar.gz::https://github.com/vmware/open-vm-tools/archive/stable-$_ver.tar.gz
	0001-lib-misc-Recognize-Alpine-Linux.patch
	0002-open-vm-tools-Add-disable-werror-configure-option.patch
	0003-Do-not-assume-that-linux-and-gnu-libc-are-the-same-t.patch
	0004-Use-configure-test-for-struct-timespec.patch
	0005-Fix-definition-of-ALLPERMS-and-ACCESSPERMS.patch
	0006-Use-configure-to-test-for-feature-instead-of-platfor.patch
	0007-Use-configure-test-for-sys-stat.h-include.patch
	0008-Rename-poll.h-to-vm_poll.h.patch
	0010-use-posix-strerror_r-unless-gnu.patch
	0011-use-off64_t-instead-of-loff_t.patch
	snprintf.patch
	strerror_r.patch
	systemLinux.c-fixup-for-Alpine.patch
	mock-res_ninit-and-res_nclose.patch
	change-default-log-dir.patch
	change-statechange.subr-location.patch
	tools.conf.patch
	use-libtirpc-nokrb.patch

	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotate
	"
builddir="$srcdir/$pkgname-stable-$_ver"
options="suid"  # needed for vmware-user-suid-wrapper

# secfixes:
#   12.3.0-r0:
#     - CVE-2023-20900
#   12.2.5-r0:
#     - CVE-2023-20867
#   12.1.0-r0:
#     - CVE-2022-31676

_subpackages="$subpackages"  # save for func _all
_pluginsdir='usr/lib/open-vm-tools/plugins'

prepare() {
	default_prepare

	cd open-vm-tools
	autoreconf -vif
}

build() {
	export CUSTOM_PROCPS_NAME="procps"
	export CFLAGS="$CFLAGS -Wno-unused-but-set-variable -D_GNU_SOURCE"
	export CXXFLAGS="$CXXFLAGS -std=gnu++11"

	cd open-vm-tools

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--with-x \
		--without-dnet \
		--without-xerces \
		--without-icu \
		--without-kernel-modules \
		--without-pam \
		--disable-werror \
		--disable-glibc-check
	make
}

check() {
	cd open-vm-tools

	make check

	checkbashisms scripts/linux/network
	checkbashisms scripts/common/statechange.sh
}

package() {
	local confdir="$pkgdir/etc/vmware-tools"
	local sharedir="$pkgdir/usr/share/$pkgname"
	local i

	make -C open-vm-tools install DESTDIR="$pkgdir"

	cd "$pkgdir"

	chmod -x "$confdir"/*.conf*
	mv "$confdir"/tools.conf.example "$confdir"/tools.conf

	# vm-support requires root privileges to run, so it should be in
	# /usr/sbin, not /usr/bin.
	mkdir -p ./usr/sbin
	mv ./usr/bin/vm-support ./usr/sbin/vm-support

	# *-vm-default scripts are identical and they are not supposed to be
	# modified by the user, so move the script to /usr/share and symlink back.
	mkdir -p "$sharedir"
	mv "$confdir"/statechange.subr "$sharedir"/
	cp -a ./etc/vmware-tools/poweroff-vm-default "$sharedir"/statechange.sh
	local path; for path in ./etc/vmware-tools/*-vm-default; do
		cmp "$path" "$sharedir"/statechange.sh \
			|| die '*-vm-default scripts are not identical!'
		rm $path
		ln -s ../../usr/share/$pkgname/statechange.sh $path
	done

	# This script works with NetworkManager only.
	# TODO: Write network script for Alpine.
	rm -f "$confdir"/scripts/vmware/network

	# We don't build with PAM support.
	rm -Rf "$pkgdir"/etc/pam.d

	# These commands allow to modify some VM's parameters or write to VM's
	# logs which is typically undesirable to be allowed to any user or
	# process. Of course, this cannot prevent users from copying and
	# running their own open-vm-tools binaries, but better than nothing...
	# See also https://github.com/vmware/open-vm-tools/issues/288.
	for i in vmtoolsd vmware-namespace-cmd vmware-rpctool \
		vmware-toolbox-cmd vmware-xferlogs;
	do
		chgrp vmware ./usr/bin/$i
		chmod 750 ./usr/bin/$i
	done

	chmod 4755 ./usr/bin/vmware-user-suid-wrapper

	install -D -m 755 "$srcdir"/$pkgname.initd ./etc/init.d/$pkgname
	install -D -m 644 "$srcdir"/$pkgname.confd ./etc/conf.d/$pkgname
	install -D -m 644 "$srcdir"/$pkgname.logrotate ./etc/logrotate.d/$pkgname
	install -d -m 750 ./var/log/vmware
}

lang() {
	pkgdesc="Languages for package $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel lang"

	amove usr/share/open-vm-tools/messages
}

deploypkg() {
	pkgdesc="DeployPkg plugin for open-vm-tools"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/lib/libDeployPkg.so.*
	amove $_pluginsdir/vmsvc/libdeployPkgPlugin.so
}

guestinfo() {
	pkgdesc="GuestInfo plugin for open-vm-tools"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_pluginsdir/vmsvc/libguestInfo.so
}

gtk() {
	pkgdesc="Xorg apps and plugins for open-vm-tools"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove etc/xdg

	amove usr/bin/vmware-user-suid-wrapper
	amove usr/bin/vmware-vmblock-fuse
	amove usr/bin/vmware-user

	amove usr/lib/open-vm-tools/plugins/vmusr
	amove usr/lib/open-vm-tools/plugins/vmsvc/libresolutionKMS.so
}

hgfs() {
	pkgdesc="Support for shared folders in VMware guests"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/bin/vmhgfs-fuse
	amove usr/bin/vmware-hgfsclient

	amove usr/lib/libhgfs.so.*
	amove $_pluginsdir/common/libhgfsServer.so
}

timesync() {
	pkgdesc="TimeSync plugin for open-vm-tools"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_pluginsdir/vmsvc/libtimeSync.so
}

vix() {
	pkgdesc="VIX plugin for open-vm-tools"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_pluginsdir/common/libvix.so
}

vmbackup() {
	pkgdesc="VmBackup plugin for open-vm-tools"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_pluginsdir/vmsvc/libvmbackup.so
}

_all() {
	pkgdesc="Meta package with all open-vm-tools plugins except gtk"
	depends=""
	local plugin

	for plugin in deploypkg guestinfo hgfs timesync vix vmbackup; do
		if ! echo "$_subpackages" | grep -qw "$pkgname-$plugin"; then
			die "plugin $plugin is missing in subpackages!"
		fi
		depends="$depends $pkgname-$plugin"
	done

	mkdir -p "$subpkgdir"
}

sha512sums="
c10e0aad49c1ca604ac738f985cca944659ac01206d3652c0ca72c693dc231e0547b8298e427f0ad460a9fb50799032813de15d628c26531b0bbcc80c93dfeb3  open-vm-tools-12.3.0.tar.gz
c2ebf6e59cef79d9e981bd571a6991ed9eae26d1934f4fecc73136c29291383c0d4bd2e38c68183a5e56900def709227e7f09b4352bbf8d2c12f879bdb1866dd  0001-lib-misc-Recognize-Alpine-Linux.patch
9188161f9bbd6572ee20855e727257b2140b39f1ba19b96879eb9878fb08cbdb7c3a5c9f5f8a4f389090acea6f71ee88fb36c7d4a74c1b8b94c9124db696a7f2  0002-open-vm-tools-Add-disable-werror-configure-option.patch
1dd0cde6ee19aec77ddf803e95b0404e803569a38c7b3d001832b4bbeae95ea15ed32c59f27bffb315130bad0c7a788a7a522df13a5f265f66c4c7c302a1966d  0003-Do-not-assume-that-linux-and-gnu-libc-are-the-same-t.patch
c647561c72b3ad919502ec4dd0a1ab736e2d479d8437b882791adce5417d8c26163ecffe9e212dab633c513bce6447cdbcb610eecf7dbb0eeeb792db16301de5  0004-Use-configure-test-for-struct-timespec.patch
6b0b9cc4a7b9837649a01aacb957b4465ec901250e48dbb273514abedf8eb3a9c16b3ad09be413a8ab897e37dff0d065a3144a2418cd7ae5aa3020bd1ed14203  0005-Fix-definition-of-ALLPERMS-and-ACCESSPERMS.patch
10a684b7c558f40bd5f22e0e369b9bbc84ade258acc13d82793529f1788a991adccdb9b9c36ad6cdcb15dc1ff8f620d7813bf910b45a460019f74ad4977af0b8  0006-Use-configure-to-test-for-feature-instead-of-platfor.patch
9d28eab3e1a40138efa05e78956a2615b171703f9689252d0960145e39b0ba5bbf0e2007479cbbca3dced89ccb45758908a57a2f5b361e4813ad67a0732e4539  0007-Use-configure-test-for-sys-stat.h-include.patch
03579b28ecc446c6a16256be8b53dcb9b5f90d05ea52aad77997f2a7957d63af546182872b2ad2ed2f99904a22be23fc64ced4ecac91a53646d9946bd6d13625  0008-Rename-poll.h-to-vm_poll.h.patch
6c8b99f51bc0fd114ce66a3311f151efbe56ec308d079afec837b9d0b60eae3fb42e1b5219246c1015a23fb63616398b958e1388830b80d5efdb27cce7bd6552  0010-use-posix-strerror_r-unless-gnu.patch
a38bb2ac16657be9f7e9b2a5e32d23e66db1f283284c1cb8e7ffe853e047373397e60f01e735a28ecfaa60010f285b0b5a4066465a2afbe643a9b4b9b74ac1f4  0011-use-off64_t-instead-of-loff_t.patch
129209e050ceed58fcfeccf7ebe0f67bb7a883c6bb4af0aef5db69e20c4b8da257a84e4e3f35ba565bedd03ece7a0d512601e95beed4bda0c41ad0a3ffeefdac  snprintf.patch
5507a58198179805e4c924b6bd6ac8b026bd602c9e5e4c52213dadf6c8880a23a16ee49a4dfe2db27515c2a36009e609dd81b7ee0d560bbea1ddf6a5324eb7ac  strerror_r.patch
c18280eb3fdfe1e3ecc5aa116369908c71917bd4dd49743a9abf78e0096e8d6c7d4323057cf506ef31886b893b2e4c4839656172edd5097a347efa96aea1c86c  systemLinux.c-fixup-for-Alpine.patch
8288503c54c5a15268b1590d747d4f246fa348734ebc70f3bd56f8cec6419d032ee6bf52a7579ee088f2c80f5b0d85f050e0615d6ad6cd8da016efa2a626ce17  mock-res_ninit-and-res_nclose.patch
0278188f5f0e486f88594ed8c921054343f05a7108d9c98d345f14362330ea028d65ede087b3d1126ac40db842decaa1fedc464de68bbec0bb333c1e133095ff  change-default-log-dir.patch
6494fe94f332706a130dd12afa18b881f6b6c4e0523570ff866e1678e37af6bd6148c4cf7d957e12f8dc2a51b4f07aa849c4e947911bcbe20d0c81049a2b6974  change-statechange.subr-location.patch
61cef02b8104b8c49119e759adefcae22ad6a21d1dea7389c8e55ae438160ca9ba98fa68ea0f8c9b26b8fd7d4e8a94ec205354e3fd22e59daf40b2e5f6f28465  tools.conf.patch
0feef314fc178922f86fdb072fc46ae0932aa59085b8263beecc1ad53c9bec04967b0d7fa62ab1051e65685b03bba80ccb075fe01c0ac67591c81f38131176b2  use-libtirpc-nokrb.patch
1248234f19a29b28b6a75886b39d1579d0f94d43925db1f709b7a05907582f6650b78d39155e7da335f27b6da00586b9c3dc3ca7e4cab19dba5c84cfefd1e6cb  open-vm-tools.initd
f996fc598d01ea778797674fca2a9f467d7ce7ced50e4e0343b3d53bea65770df488b1a286e42603d91e05ba85610161eb68e3fbaa5cbf1757a8aec81869a677  open-vm-tools.confd
4b79d7432f366e5ef202e69bffed5caacab8008f39475d50d54cfa5ac9f521cc0df7122d8e3a95340a4b00cb0b202717293df14f9d411d5edf41dcc7fa4c0550  open-vm-tools.logrotate
"
